var app = angular.module('myApp.filters', []);

app.filter('html', ["$sce", function ($sce) {
    return function (input) {
        return $sce.trustAsHtml(input);
    }
}]);

app.filter('background', [function () {
    return function (input) {
        if (!input) return "";
        return {'background-image': 'url(' + input + ')'}
    }
}]);

//arrSplit
app.filter('arrSplit', [function () {
    return function (input, size) {
        if (!input || !input.length) return [];
        size = size || 3;
        var arr = angular.copy(input);
        if (arr.length > size) arr.length = size;
        return arr;
    }
}]);

//时间转化
app.filter('dateDiff', ["$filter", function ($filter) {
    return function (dateTimeStamp) {
        if (!dateTimeStamp) return "未知";
        dateTimeStamp = parseInt(dateTimeStamp, 10);
        if (isNaN(dateTimeStamp)) {
            return "未知";
        }

        dateTimeStamp = dateTimeStamp * 1000; //转JS时间戳

        var minute = 1000 * 60;
        var hour = minute * 60;
        var day = hour * 24;
        // var halfamonth = day * 15;
        var month = day * 30;
        var now = new Date().getTime();
        var diffValue = now - dateTimeStamp;
        if (diffValue < 0) {
            return;
        }
        var monthC = diffValue / month;
        var weekC = diffValue / (7 * day);
        var dayC = diffValue / day;
        var hourC = diffValue / hour;
        var minC = diffValue / minute;
        var result;

        // if (monthC > 1) {
        //     result = $filter('date')(dateTimeStamp, 'yyyy.MM.dd');
        // } else if (monthC == 1) {
        //     result = "" + parseInt(monthC) + "月前";
        // } else if (weekC >= 1) {
        //     result = "" + parseInt(weekC) + "周前";
        // } else if (dayC >= 1) {
        //     result = "" + parseInt(dayC) + "天前";
        // }


        if (dayC > 7) {
            result = $filter('date')(dateTimeStamp, 'yy-MM-dd');
        } else if (dayC >= 1 && dayC <= 7) {
            result = "" + parseInt(dayC) + "天前";
        } else if (hourC >= 1) {
            result = "" + parseInt(hourC) + "小时前";
        } else if (minC >= 1) {
            result = "" + parseInt(minC) + "分钟前";
        } else
            result = "刚刚";
        return result;
    }
}]);

//php 时间戳
app.filter('phpToJsData', [function () {
    return function (dateTimeStamp) {
        if (!dateTimeStamp) return 1;

        dateTimeStamp = parseInt(dateTimeStamp, 10);
        if (isNaN(dateTimeStamp)) {
            return 1;
        }
        dateTimeStamp = dateTimeStamp * 1000; //转JS时间戳
        return dateTimeStamp;
    }
}]);
